#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_CD_FOLD_ADD	(0)
#define ITEM_CD_FOLD_SUB	(1)
#define ITEM_CD_SCAN		(2)
#define ITEM_CD_RPT		(3)
#define ITEM_CD_RND		(4)
#define ITEM_CD_EXIT		(5)


TRect		CDTimeInfoPos= {154,87,562,262};
TRect		CDTrkInfoPos= {154,262,562,309};
TRect		CDFoldInfoPos= {154,308,562,360};
TRect		MenuCdTouchRect[IMG_MENU_CD_ITEM_QTY];
TPosCoor	MenuCdItemPos[IMG_MENU_CD_ITEM_QTY] = { {8,87}, {8,189}, {9,375}, {150,375}, {293,375}, {577,375} };
TPosCoor	MenuCdDisInfoPos= {145,170};//{260,204}; ////ok___20080902
//TPosCoor	MenuCdDisInfoPos= {217,168}; //test
TPosCoor	MenuCdTracInfoPos= {268,344};
TPosCoor	MenuCdRPTFlagPos= {294,2};
int		MenuCdFocus = -1;
TFrame		MenuCdFrame;
char		MenuCdModeStr[40];
char* 	MenuCdStr[6] = {"","","SCAN","RPT","RND","EXIT"};/*{"FOLD+","FOLD-","SCAN","RPT","RND","EXIT"};*/
char 		MenuCdItemStr [10] = {'\0'};
//unsigned char CD_data [5] = {0xe0,0x03,0x04,0x02,0xe9};
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
static int 		exit_flag = 0;//0:exit is valid,1:exit is invalid.
static unsigned char		MenuCDTrack[15] = {"Trk    "};///////////////if changed to this,then ok.
static unsigned char		MenuCDFold[15] = {"Fld    "};
static int  TotFold =0, TotTrk =0, CurFold =0,CurTrk =0;
extern  unsigned char send_data_buf[5];
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;
extern int 	Last_ID ;
extern unsigned char mediea_flag;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;


struct _CDMP3Info
{
	char		MenuCDTr[10];
	char		MenuCDTime[6];
	unsigned char 		total_fold;
	unsigned char 	 	cur_fold;
	unsigned char 		trac;
	unsigned char 		cd_flag;	//0 is  is loading,01:normal disp,02:no disc,03:disc err,04:eject 
	unsigned char 		last_cd_flag;
	unsigned char 		button_flag[5] ;//0 is valid ,1 is invalid. button_flag[0]is fold ,button_flag[1]exit ,
																		//0 is normal,1 is hilight,button_flag[2]scan,button_flag[3]repreat,button_flag[4]radom
	short	CDMP3Flag;//0 is cd ,1 is mp3
};

static struct _CDMP3Info	CDMP3Info;

void MenuCDInitTimeTr(char val )
{
				CDMP3Info.MenuCDTime[0] = '0';
				CDMP3Info.MenuCDTime[1] = '0';
				CDMP3Info.MenuCDTime[2] = ':';
				CDMP3Info.MenuCDTime[3] = '0';
				CDMP3Info.MenuCDTime[4] = '0';
				CDMP3Info.MenuCDTime[5] = '\0';
				
			if (val)
				{
				MenuCDTrack [0] = 'T';
				MenuCDTrack [1] = 'r';
				MenuCDTrack [2] = 'k';
				MenuCDTrack [3] = ' ';
				MenuCDTrack [4] = ' ';
				MenuCDTrack [5] = ' ';
				MenuCDTrack [6] = ' ';
				MenuCDTrack [7] = '\0';
				}
				CDMP3Info.button_flag[2] = 0;
				CDMP3Info.button_flag[3] = 0;
				CDMP3Info.button_flag[4] = 0;
}

void MenuCDHilightFlagInit(void)
{
		CDMP3Info.button_flag[2] = 0;
		CDMP3Info.button_flag[3] = 0;
		CDMP3Info.button_flag[4] = 0;
}

int  CDInfoUpdata (void)
{
	int 	play_time_minu,play_time_sec;
	int   tr;
	tr = read_info_data[4] * 256 + read_info_data[5];	
	
	
	if (read_info_data[2] == ID_SCREEN_INFO)
		{
		
				////////////  need a total folder ,CD ,MP3 INFO//////////////
			switch (read_info_data[3])
				{
					case	ID_SCREEN_INFO_CURFLOD:
						CurFold = tr;	
					  printf ("in receive, TotFold is %d,curfold is %d\n",TotFold,CurFold);
						break;
			
					case	ID_SCREEN_INFO_CURTRK:	
						CurTrk = tr;
						break;
			
					case	ID_SCREEN_INFO_CURTIME:	
						play_time_minu =  read_info_data[4];//minute
						play_time_sec = read_info_data[5];
						CDMP3Info.MenuCDTime[0] = '0' + play_time_minu/10;
						CDMP3Info.MenuCDTime[1] = '0' + play_time_minu%10;
						CDMP3Info.MenuCDTime[2] = ':';
						CDMP3Info.MenuCDTime[3] = '0' + play_time_sec/10;
						CDMP3Info.MenuCDTime[4] = '0' + play_time_sec%10;
						CDMP3Info.MenuCDTime[5] = '\0';
						break;
					case ID_SCREEN_INFO_TOTFLOD:
						TotFold = tr;
						break;
					case ID_SCREEN_INFO_TOTTRK:
						TotTrk = tr;
						break;
				}
		}
		if (read_info_data[2] == ID_DISPLAY_SCREEN)
		{
				switch (read_info_data[3])
								{													
									case	ID_SCREEN_CDLOAD:	
										CDMP3Info.cd_flag = 0;
										read_info_data[0] = 0;
										MenuCDInitTimeTr(0);	
										MenuCDHilightFlagInit();																														
										break;									
									case	ID_SCREEN_CDDISP:
										CDMP3Info.CDMP3Flag = 0;//cd
										CDMP3Info.cd_flag = 1;										
										read_info_data[0] = 0;	
										//read_info_data[1] = 0;																	
										break;
									case	ID_SCREEN_MP3DISP:
										CDMP3Info.CDMP3Flag = 1;//mp3									
										CDMP3Info.cd_flag = 1;																	
										read_info_data[0] = 0;	
										//read_info_data[1] = 0;									
										break;
									case	ID_SCREEN_NODISC:										
										CDMP3Info.cd_flag = 2;										
										read_info_data[0] = 0;	
										TotFold = 0;
										break;
									case	ID_SCREEN_DISCERR:
										CDMP3Info.cd_flag = 3;										
										read_info_data[0] = 0;																
										break;
									case	ID_SCREEN_EJECT:
										CDMP3Info.cd_flag = 4;																		
										read_info_data[0] = 0;										
										break;
							}
		 }
		 
		 if ( (read_info_data[2] == ID_KEY_STATUS)&& (read_info_data[3] == 0x02))
			{	
				//printf ("in cd  hilight handle!!\n");	
						
				switch (read_info_data[4])
				{
				case 		0x01:
						CDMP3Info.button_flag[1] = 1;		//exit is invalid						
					break;
				case 		0x02:
						CDMP3Info.button_flag[1] = 0;
					break;	
				case 		0x03:
					CDMP3Info.button_flag[0] = 1;			//fold is invalid			
					break;	
				case 		0x04:
						CDMP3Info.button_flag[0] = 0;
					break;
				case 		0x07:
					CDMP3Info.button_flag[2] = 1;			//scan is hilight			
					break;	
				case 		0x08:
						CDMP3Info.button_flag[2] = 0;
					break;
				case 		0x09:
						CDMP3Info.button_flag[3] = 1;		//repeat is hilight								
					break;
				case 		0x0a:
						CDMP3Info.button_flag[3] = 0;
					break;	
				case 		0x0b:
					CDMP3Info.button_flag[4] = 1;			//random is hilight			
					break;	
				case 		0x0c:
						CDMP3Info.button_flag[4] = 0;
					break;		
				}		
			}	
		 return 0;
}


void MenuCDDrawWaiting(TCanvas *pCanvas)
{	
		int GetJpegIdx;
		char FileName[20];
		
		DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	switch (CDMP3Info.cd_flag)
	{
		case 	00:
			GetJpegIdx = IMG_MENU_CD_LOAD_INFO;		
			break;
		case  02:
			GetJpegIdx = IMG_MENU_CD_NODISC_INFO;
			break;
		case  03:
			GetJpegIdx = IMG_MENU_CD_DISCERROR_INFO;
			break;
		case  04:
			GetJpegIdx = IMG_MENU_CD_EJECT_INFO;
			break;			
	}
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_CD_FOLDER, GetJpegIdx);
	DrawJpeg(pCanvas, MenuCdDisInfoPos.x, MenuCdDisInfoPos.y,FileName);

}

void MenuDrawCDMP3Info( TCanvas *pCanvas)
{
	//int GetJpegIdx;
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_GRAY, 0, 0 );	
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	if (CDMP3Info.CDMP3Flag)		
		g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "MP3" );
	else
		g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "CD" );
}


void MenuCDDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}	
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;		
					printf ("y is %d \n",y);
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				MenuDrawCDMP3Info( MenuCdFrame.pCanvas );	
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				MenuCDDrawVolume(MenuCdFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void MenuCdDrawInfo( TCanvas *pCanvas )
{
		
	char FileName[20];
	int GetJpegIdx;

	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE,0, 0 );	
	/*if (CDMP3Info.CDMP3Flag)
		{
		GetJpegIdx = IMG_MENU_CDMP3_DISP_UP;
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_CD_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, CDTimeInfoPos.left, CDTimeInfoPos.top,FileName);
		g_Puts( pCanvas, FONT33, CDTimeInfoPos, TEXT_ALIGN_CENTER,CDMP3Info.MenuCDTime);
		}
	else*/
		{
		GetJpegIdx = IMG_MENU_CD_DISP_UP;
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_CD_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, CDTimeInfoPos.left, CDTimeInfoPos.top,FileName);
		g_Puts( pCanvas, FONT33, CDTimeInfoPos, TEXT_ALIGN_CENTER,CDMP3Info.MenuCDTime);
		}
}



	

void MenuCdDrawTrInfo( TCanvas *pCanvas)
{
	
	//TJpeg pJpeg;
	char FileName[20];
	int GetJpegIdx;	
	
		
	
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_GRAY, 0, 0 );	
/*	if (CDMP3Info.CDMP3Flag)
		{
		GetJpegIdx = IMG_MENU_CDMP3_DISP_TRK;
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_CD_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, CDTrkInfoPos.left, CDTrkInfoPos.top,FileName);
		g_Puts( pCanvas, FONT33, CDTrkInfoPos, TEXT_ALIGN_CENTER,MenuCDTrack);
		
		GetJpegIdx = IMG_MENU_CDMP3_DISP_FOLD;
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_CD_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, CDFoldInfoPos.left, CDFoldInfoPos.top,FileName);
		g_Puts( pCanvas, FONT33, CDFoldInfoPos, TEXT_ALIGN_CENTER,MenuCDFold);
		}
	else
	*/
		{
		GetJpegIdx = IMG_MENU_CD_DISP_TRK;
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_CD_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, CDTrkInfoPos.left, CDTrkInfoPos.top,FileName);
		sprintf( MenuCDTrack, "Trk %d/%d", CurTrk, TotTrk); 
		g_Puts( pCanvas, FONT33, CDTrkInfoPos, TEXT_ALIGN_CENTER,MenuCDTrack);

		
		GetJpegIdx = IMG_MENU_CD_DISP_FOLD;
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_CD_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, CDFoldInfoPos.left, CDFoldInfoPos.top,FileName);
		if (TotFold >0)
			{
			if (!CurFold)
				CurFold= 1;
			sprintf( MenuCDFold, "Fld %d/%d", CurFold, TotFold); 
			}
		else
			{
			sprintf( MenuCDFold, "  "); 			
			}
		g_Puts( pCanvas, FONT33, CDFoldInfoPos, TEXT_ALIGN_CENTER,MenuCDFold);
		
		}

}

void CdDrawUnit (TCanvas *pCanvas,int idx, int no)//,BOOL bfocus)
{
	TJpeg pJpeg;
	TPosCoor pos;	
	char FileName[20];
	int GetJpegIdx;
			
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_GRAY, 0, 0 );
	GetJpegIdx = no;
	pos = MenuCdItemPos[idx];		
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_CD_FOLDER, GetJpegIdx);
	DrawJpeg(pCanvas, pos.x, pos.y,FileName);

	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	MenuCdTouchRect[idx].left = pos.x;
	MenuCdTouchRect[idx].right = pos.x + pJpeg.Width;
	MenuCdTouchRect[idx].top = pos.y;
	MenuCdTouchRect[idx].bottom = pos.y + pJpeg.Height;
		
	g_Puts( pCanvas, FONT33, MenuCdTouchRect[idx], TEXT_ALIGN_CENTER, MenuCdStr[idx]);//MenuCdItemStr);

		
}


void MenuCdDrawBackgroup(TCanvas *pCanvas)
{
	//DrawJpeg(pCanvas, 0, 0, "/res_jpeg/menu_cd.jpg" );
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );	
}

////// can be changed /////////////
void MenuCdDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{	
	int no;
	
	if( idx < 0 || idx >= IMG_MENU_CD_ITEM_QTY )
		return;

		switch ( idx)
		{
			case ITEM_CD_FOLD_ADD:
				if (CDMP3Info.button_flag[0])
					{
						//sprintf (MenuCdItemStr,MenuCdStr[idx]);//sprintf (MenuCdItemStr,"FOLD+");
						CdDrawUnit (pCanvas, idx,	IMG_MENU_CD_ITEM_FOLD_PLUS_VALID);	
					}
				else
					{
						//sprintf (MenuCdItemStr,MenuCdStr[idx]);//memset(MenuCdItemStr,0,10);
						if( bfocus )
							no = IMG_MENU_CD_ITEM_FOCUS_FOLD_PLUS;
						else
							no =  IMG_MENU_CD_ITEM_FOLD_PLUS ;
						CdDrawUnit (pCanvas,idx, no);
					}	
				break;
				
			case ITEM_CD_FOLD_SUB:
				if (CDMP3Info.button_flag[0])
					{
							//sprintf (MenuCdItemStr,MenuCdStr[idx]);
							CdDrawUnit (pCanvas, idx,	IMG_MENU_CD_ITEM_FOLD_MINUS_VALID);
					}
					else
						{
							//sprintf (MenuCdItemStr,MenuCdStr[idx]);//memset(MenuCdItemStr,0,10);
						if( bfocus )
							no = IMG_MENU_CD_ITEM_FOCUS_FOLD_MINUS;
						else
							no =  IMG_MENU_CD_ITEM_FOLD_MINUS;
						CdDrawUnit (pCanvas,idx, no);
						}
						break;

			case ITEM_CD_EXIT:
				
				if (CDMP3Info.button_flag[1])
					{
						//sprintf (MenuCdItemStr,MenuCdStr[idx]);
						CdDrawUnit (pCanvas, idx,	IMG_MENU_CD_ITEM_EXIT_VALID);
					}
					else
						{
						//sprintf (MenuCdItemStr,MenuCdStr[idx]);//memset(MenuCdItemStr,0,10);
						if( bfocus )
							no =IMG_MENU_CD_ITEM_FOCUS_EXIT;
						else
							no =  IMG_MENU_CD_ITEM_EXIT ;
						CdDrawUnit (pCanvas,idx, no);
						}		
				break;
			
				
		default:
				if (idx <=ITEM_CD_EXIT)
					{
						//sprintf (MenuCdItemStr,MenuCdStr[idx]);//memset(MenuCdItemStr,0,10);
						if (CDMP3Info.button_flag[idx])
							{
								no =IMG_MENU_CD_ITEM_FOCUS_EXIT;
							}
							else
								{
								if( bfocus )
									no = IMG_MENU_CD_ITEM_FOCUS_EXIT;
								else
									no =  IMG_MENU_CD_ITEM_EXIT ;
									//printf ("in MenuCdDrawItem222!\n");
								}
						CdDrawUnit (pCanvas,idx, no);//, BOOL bfocus);
					}
				break;
		}
}


void MenuCdDraw( BOOL bDrawAll )
{
	int i;
	if (CDMP3Info.cd_flag != 1)
		{
			MenuCDDrawWaiting(MenuCdFrame.pCanvas);	
			dtpresent_flag++;		
			pthread_cond_signal(&dt_cond);					
			return ;				
		}
	rece_handle = 1;////////////////20080107
	if( bDrawAll )
		{		
		MenuCdDrawBackgroup(MenuCdFrame.pCanvas);
		}
	
	for( i = 0; i < IMG_MENU_CD_ITEM_QTY; i++ )
		MenuCdDrawItem( MenuCdFrame.pCanvas, i, FALSE );
	MenuCdDrawInfo( MenuCdFrame.pCanvas );
	MenuDrawCDMP3Info( MenuCdFrame.pCanvas );
	MenuCdDrawTrInfo( MenuCdFrame.pCanvas );
		if (mute_status == 1)
		{
			MenuCDDrawVolume(MenuCdFrame.pCanvas,1);	
		}
		
	//dtpresent_flag++;		
	//pthread_cond_signal(&dt_cond);
	//printf(" ____________in dtpresent flag 20080227\n");
	dt_Present();////////////20080227
	rece_handle = 0;
}

int MenuCdGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_MENU_CD_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, MenuCdTouchRect[i] ) )
			return i;
	return -1;
}

BOOL MenuCdBtnEnter(int idx)
{
	switch(idx)
	{
		case ITEM_CD_FOLD_ADD:
		case ITEM_CD_FOLD_SUB:
		case ITEM_CD_SCAN:
			MenuCdDrawItem( MenuCdFrame.pCanvas, idx, FALSE );
			dt_Present_part(MenuCdTouchRect [idx]);	
			//dt_Present();
			break;
		case ITEM_CD_RPT:
			//ReplayFlag = !ReplayFlag;			
			MenuCdDrawItem( MenuCdFrame.pCanvas, idx, FALSE );
			MenuCdDrawInfo( MenuCdFrame.pCanvas );
			dt_Present_part(MenuCdTouchRect [idx]);	
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);	
			//dt_Present();
			break;
		case ITEM_CD_RND:
			//RandomFlag = !RandomFlag;			
			MenuCdDrawItem( MenuCdFrame.pCanvas, idx, FALSE );
			MenuCdDrawInfo( MenuCdFrame.pCanvas );//, MenuCdModeStr );
			dt_Present_part(MenuCdTouchRect [idx]);	
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);	
			//dt_Present();
			break;
		case ITEM_CD_EXIT:
			MenuMainInit();
			break;
		
	}
	LOG( "btn = %d\n", idx );	
	return TRUE;
}

void MenuCdInit()
{
	ir_SetCurrentID( IR_MENU_CD_INIT );
	//MenuCDHilightFlagInit();/////////////20080219
	//rece_handle = 1;
	mediea_flag = 0;
	frm_Create( &MenuCdFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	MenuCdDraw( TRUE );
	printf("in menu cd init!\n");
	//rece_handle = 0;
}
/*
static int send_data_out ( int index)
{
	
	
	if ((index < 0) || (index > IMG_MENU_CD_ITEM_QTY))
		return -1;
		
		CD_data [3] = index+ 0x02;
		CD_data [4] = (unsigned char )(0xe9 + index);
		if (dev_SPIWrite(CD_data,5)==0)
			return 0;//hangsheng 071126
		else
			return -1;
		return -1;
}
*/
static int send_data (int index)
{
	if ((index < 0) || (index > IMG_MENU_CD_ITEM_QTY))
		return -1;		
			
		send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;		
		send_data_buf [2] = IDDATA_CD;//0x02 + offset;
		send_data_buf [3] = index+ CD_FOLD_PLUS;
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		
}


/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;

	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:	
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
										printf ("in cd,handle MenuRadioInit\n");
											MenuRadioInit();
											
										break;									
									//case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									//case	ID_SCREEN_NODISC:
									//case	ID_SCREEN_DISCERR:
									//case	ID_SCREEN_EJECT:
											if  (CDMP3Info.cd_flag == 1)
												{
													MenuCdDraw( TRUE );
													printf("in CDMP3Info.cd_flag is 1 111\n");													
												}
										//rece_handle = 0;
										//MenuCdBtnEnter(ITEM_CD_UPDATE);								
									
										break;
									case	ID_SCREEN_MEDIEA:
											switch (Last_ID)
												{
													case  IR_MEDIA_PHOTO_INIT:
														MediaPhotoInit();
														break;
													case IR_MEDIA_MUSIC_INIT:
														MediaMusicInit();
														break;
													case IR_MEDIA_MOVIE_INIT:
														MediaMovieInit();
														break;
													default:
														MenuMediaInit();													
												}	
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
									read_info_data[0] = 0;													
						break;
						
					case	ID_KEY_STATUS:						
						switch (read_info_data[3])
										{
											case  0x01:
											case	0x05://stereo logo
												//MenuRadioInit();///////////////20080121
												break;
											case	0x02:
												if  (CDMP3Info.cd_flag == 1)
												{
													int i;
														for( i = 0; i < IMG_MENU_CD_ITEM_QTY; i++ )
														{
															MenuCdDrawItem( MenuCdFrame.pCanvas, i, FALSE );	
															dt_Present_part(MenuCdTouchRect [i]);		
														}																		
												}
												break;
											case	0x03:
												//rc = MusicInfoUpdata();
												//rc = MovieInfoUpdata();
												break;
											case	0x04:
												//rc = NaviInfoUpdata();
												break;											
											}
							
							read_info_data[0] = 0;	
							rc = 0;			
						break;
						
					case	ID_SCREEN_INFO:
								if (read_info_data[3] == ID_SCREEN_INFO_VOLUME )
								{
									MenuCDDrawVolume(MenuCdFrame.pCanvas,1);
									read_info_data[0] = 0;	
									break;
								}
								else
									if ((read_info_data[3] > ID_SCREEN_INFO_CUR_FRQ) && (read_info_data[3] < ID_SCREEN_INFO_BAS))
										{
											
											MenuCdDrawInfo( MenuCdFrame.pCanvas);
											MenuCdDrawTrInfo( MenuCdFrame.pCanvas );///////////// 20071209 add
											if (( ir_GetCurrentID() == IR_MENU_CD_INIT) && (CDMP3Info.cd_flag == 1) )
											{											 
											  dt_Present_part(CDTimeInfoPos );
											  dt_Present_part(CDTrkInfoPos );
											  dt_Present_part(CDFoldInfoPos);	
											  printf ("in ID_SCREEN_INFO, TotFold is %d,curfold is %d\n",TotFold,CurFold);
											}
										else
											{
												CDMP3Info.cd_flag = 1;
												MenuCdDraw( TRUE );	
												dtpresent_flag++;		
											  pthread_cond_signal(&dt_cond);											  
											  printf ("in dt_Present all \n");
											  printf ("Tr is : %c,%c,%c\n",MenuCDTrack [0],MenuCDTrack [5],MenuCDTrack [6]);
											}
											
											//dtpresent_flag++;		
											//pthread_cond_signal(&dt_cond);	
										}
										/*else
											if (read_info_data[3] < 0x08 )
											{
												MenuRadioInit();
												read_info_data[0] = 0;	
												break;
											}
									*/
									//dt_Present();		/////////////////////20070121						
								read_info_data[0] = 0;	
								rc = 0;//if handled, clear this flag.
								break;
						
					case	ID_KEYPAD:	
						switch (read_info_data[3])
							{
									case 	ID_MUTE:	
										MenuCDDrawVolume(MenuCdFrame.pCanvas,1);
										break;
									case	ID_DMUTE:	
										MenuCDDrawVolume(MenuCdFrame.pCanvas,3);
										break;	
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
										
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:	
									case 	ID_FMAM:
										MenuRadioInit();
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();								
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.							
						break;						
					case 	ID_BAND_SEL:
							//MenuRadioInit();//////////////20080121
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;			
						break;		
						
					default:	
							rc = -1;						
						}	
	return 0;			
}
void MenuCdInput(UInt32 key)
{
		int flag;		
	
		if (( key == IRC_READ_INFO) && (read_info_data[1] != 0))
		{			
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}				
		}

	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		if (CDMP3Info.cd_flag !=1 )
			{
				return ;//20080729 add
			}
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if(( key == IRC_TOUCH_DOWN ) && ( LastKey ==IRC_TOUCH_UP ))
		{			
			if( ( MenuCdFocus = MenuCdGetTouchID( pos )) != -1 )
				{
					MenuCdDrawItem( MenuCdFrame.pCanvas, MenuCdFocus, TRUE );
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);	
					dt_Present_part(MenuCdTouchRect [MenuCdFocus]);	
					//dt_Present();
					
					if ( send_data(MenuCdFocus))
						printf ("write failure !\n");	
				}			
		}
		else
		{
			if( (( focus = MenuCdGetTouchID( pos ) ) == MenuCdFocus )||(MenuCdFocus != -1 ))
				{
					if (( MenuCdFocus >ITEM_CD_FOLD_SUB)&& (MenuCdFocus <ITEM_CD_EXIT))				
						MenuCdBtnEnter( MenuCdFocus );
					else
						#ifdef INVALID_KEY
						if ((( MenuCdFocus <ITEM_CD_SCAN) && (!CDMP3Info.button_flag[0]))|| (( MenuCdFocus ==ITEM_CD_EXIT) && (!CDMP3Info.button_flag[1])))
						#endif
						{
							printf ("in CD button MenuCdBtnEnter,button is %d\n",MenuCdFocus);
							MenuCdBtnEnter( MenuCdFocus );
						}
				}	
			MenuCdFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}

